package com.auto.learning.fragment;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.R.integer;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.MainActivity;
import com.auto.learning.R;
import com.auto.learning.activity.CollectReadActivity;
import com.auto.learning.activity.PlayActivity;
import com.auto.learning.adapter.CollectPageAdapter;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.CacheTypeBean;
import com.auto.learning.bean.SelectionVo;
import com.auto.learning.event.DownLoadRefreshEvent;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.Handle;
import com.auto.learning.event.listener.DialogListener;
import com.auto.learning.net.DownLoadInfo;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.store.core.StaticReference;
import com.auto.learning.thread.pool.Pool;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.FileUtil;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.CommonAlertDialog;
import com.auto.learning.view.SharedPreferencesUtils;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.dao.RawRowMapper;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;
/**
 * 藏书页面----书架版本
 * @author wangjianjun
 */
public class CollectPageFragment extends Fragment {
	
	private final static int GETCACHE_SUCCESS = 1;
	private final static int DELETECACHE_SUCCESS = 3;
	private final static int GOTO_HAS_READ_LIST = 4;
	private final static int COLLECT_PLAY = 5;
	private final static int LONG_CLICK = 6;
	
	private TextView tv_operator_finish,tv_all_choose,tv_cancel,tv_all_delete;
	private GridView gv_booklist;
	private RelativeLayout rel_x,rel_top,relative_main,rel_empty,rel_all_has_read;
	private ImageView image_bg,image_tip,image_goto_home;

	private ArrayList<BookBean> list = new ArrayList<BookBean>();
	private ArrayList<String> imageUrl = new ArrayList<String>();
	CollectPageAdapter adapter;
	private int type = 1; //type = 1 ,主界面显示的， 2：已读书架
	boolean isNeedRefreshMain = false;
	
	public boolean isNeedRefreshMain() {
		return isNeedRefreshMain;
	}

	public void setNeedRefreshMain(boolean isNeedRefreshMain) {
		this.isNeedRefreshMain = isNeedRefreshMain;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	Handle handle = new Handle(getActivity()){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case GETCACHE_SUCCESS:
				ArrayList<BookBean> mList = (ArrayList<BookBean>) msg.obj;
				if (mList != null && mList.size() > 0) {
					list.clear();
					imageUrl.clear();
					for (BookBean bookBean : mList) {
						if (type == 1) {
							if (bookBean.getIsRead() == 1) {
								list.add(bookBean);
							}else if (bookBean.getIsRead() == 2) {
								if (imageUrl.size() < 5) {
									imageUrl.add(bookBean.getFaceImg());
								}
							}
						}else {
							bookBean.setIsRead(1);
							list.add(bookBean);
						}
					}
					if (imageUrl != null && imageUrl.size() > 0 && list.size() == 0) {
						rel_all_has_read.setVisibility(View.VISIBLE);
					}else {
						rel_all_has_read.setVisibility(View.GONE);
					}
					rel_empty.setVisibility(View.GONE);
				}else {
					list.clear();
					imageUrl.clear();
					rel_all_has_read.setVisibility(View.GONE);
					rel_empty.setVisibility(View.VISIBLE);
				}
				addEmptyItem();
				if (adapter !=  null) {
					adapter.notifyDataSetChanged();
				}
				break;
			case 2: //点击CheckBox
				if (msg.arg1 > 0) {
					tv_all_delete.setText(String.format("删除(%s)", msg.arg1));
				}else {
					tv_all_delete.setText("删除");
				}
				break;
			case DELETECACHE_SUCCESS:
				List<Integer> idList = (List<Integer>) msg.obj;
				for (int i = 0; i < list.size(); i++) {
					if (idList.contains(list.get(i).getBookId()) || list.get(i).getBookId() == 0) {
						BookBean mBean = list.remove(i);
						i--;
						if (mBean.getBookId() > 0) {
							try {
								String mPath = FileUtil.getFileCachePath() + DownLoadInfo.DOWNLOAD + File.separator + mBean.getBookId() + File.separator;
								File mFile = new File(mPath);
								if (mFile.exists() && mFile.isDirectory()) {
									File[] files = mFile.listFiles();
									for (int j = 0; j < files.length; j++) {
										if (files[j].isFile()) {
											files[j].delete();
										}
									}
									mFile.delete();
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
				if (type == 2) {
					isNeedRefreshMain = true;
				}
				EventBus.getEventBus("downloadRefresh").post(0);
				
				
				addEmptyItem();
				
				if (adapter != null && adapter.isShowCheckBox()) {
					adapter.setShowCheckBox(false);
					operator(false);
					adapter.clearSelected();
					adapter.notifyDataSetChanged();
					if ((imageUrl != null && imageUrl.size() == 0) && list.size() == 0) {
						rel_empty.setVisibility(View.VISIBLE);
					}else {
						rel_empty.setVisibility(View.GONE);
						if (imageUrl != null && imageUrl.size() > 0 && list.size() == 0) {
							rel_all_has_read.setVisibility(View.VISIBLE);
						}else {
							rel_all_has_read.setVisibility(View.GONE);
						}
					}
				}
				break;
			 case GOTO_HAS_READ_LIST:
				 if (adapter != null && !adapter.isShowCheckBox()) {
					 Intent intent = new Intent(getActivity(), CollectReadActivity.class);
					 getActivity().startActivityForResult(intent, 660);
				 }
				break;
			 case LONG_CLICK:
				 longClick(msg.arg1);
				 break;
			 case COLLECT_PLAY:
				 int position = msg.arg1;
				 final int index = (imageUrl != null && imageUrl.size() > 0) ? position - 1 : position;
				 MusicPlayManager manager = MusicPlayManager.getInstance(getActivity());
				 int id_s[] = manager.isPlayingBookId();
				 if (id_s[0] == 0 || id_s[0] != list.get(index).getBookId()) {
					 if (type == 2) {
					 	TemporaryVariable.read_list.clear();
						TemporaryVariable.read_list.add(list.get(index).getBookId());
						if (TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
							TemporaryVariable.playActivity.finish();
						}
						Intent mIntent = new Intent(getActivity(), PlayActivity.class);
						mIntent.putExtra("bookId", list.get(index).getBookId());
						mIntent.putExtra("faceImg", list.get(index).getFaceImg());
						mIntent.putExtra("canRead", list.get(index).getCanRead());
						mIntent.putExtra("needBuy", list.get(index).getNeedBuy());
						getActivity().startActivity(mIntent);
						handle.postDelayed(new Runnable() {
							
							@Override
							public void run() {
								ArrayList<BookBean> m_list = new ArrayList<BookBean>();
								m_list.add(list.get(index));
								MusicPlayManager.getInstance(getActivity()).setPlayList(m_list).setBookId(list.get(index).getBookId()).play();
							}
						}, 1500);
					}else{
						 playDialog(list.get(index));
					}
				 }else {
					manager.pause();
				 }
				 break;
			}
		};
	};
	
	public void addEmptyItem(){
		if (list != null && list.size() > 0 && list.get(0).getBookId() > 0) {
			int listCount = list.size() + ((imageUrl != null && imageUrl.size() > 0) ? 1 : 0);
			int addCount = 0;
			switch (listCount % 3) {
			case 1:
				addCount = 2;
				break;
			case 2:
				addCount = 1;
				break;
			}
			if (addCount > 0) {
				for (int i = 0; i < addCount; i++) {
					list.add(new BookBean());
				}
			}
		}else {
			list.clear();
			if (imageUrl != null && imageUrl.size() > 0) {
				for (int i = 0; i < 2; i++) {
					list.add(new BookBean());
				}
			}
		}
	}
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.collect_fragment_3, null);
		initView(view);
		registerEvent();
		getDownLoadList();
		return view;
	}
	
	private void initView(View view){
		rel_all_has_read = (RelativeLayout) view.findViewById(R.id.rel_all_has_read);
		rel_empty = (RelativeLayout) view.findViewById(R.id.rel_empty);
		image_goto_home = (ImageView) view.findViewById(R.id.image_goto_home);
		tv_operator_finish = (TextView) view.findViewById(R.id.tv_operator_finish);
		tv_all_choose = (TextView) view.findViewById(R.id.tv_all_choose);
		tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
		tv_all_delete = (TextView) view.findViewById(R.id.tv_all_delete);
		gv_booklist = (GridView) view.findViewById(R.id.gv_booklist);
		rel_x = (RelativeLayout) view.findViewById(R.id.rel_x);
		rel_top = (RelativeLayout) view.findViewById(R.id.rel_top);
		rel_x.setVisibility(View.GONE);
		rel_top.setVisibility(View.GONE);
		relative_main = (RelativeLayout) view.findViewById(R.id.relative_main);
		image_bg = (ImageView) view.findViewById(R.id.image_bg);
		image_tip = (ImageView) view.findViewById(R.id.image_tip);
		image_bg.setImageBitmap(CommonMethod.readBitMap(getActivity(), R.drawable.favoritebg));
		if ((boolean)SharedPreferencesUtils.getParam(getActivity(), "m_collcet_show_tip", true)) {
			image_tip.setVisibility(View.VISIBLE);
			image_tip.setImageBitmap(CommonMethod.readBitMap(getActivity(), R.drawable.collect_tip));
		}
		image_goto_home.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (type == 1) {
					((MainActivity)getActivity()).gotoHome();
				}else {
					if (getActivity() instanceof CollectReadActivity) {
						((CollectReadActivity)getActivity()).setResult(661);
						((CollectReadActivity)getActivity()).finish();
					}
				}
			}
		});
		image_tip.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				image_tip.setVisibility(View.GONE);
				SharedPreferencesUtils.setParam(getActivity(), "m_collcet_show_tip", false);
			}
		});
		gv_booklist.setSelector(new ColorDrawable(Color.TRANSPARENT));
		adapter = new CollectPageAdapter(getActivity(), list, imageUrl, handle);
		adapter.setType(type);
		gv_booklist.setAdapter(adapter);
		gv_booklist.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				longClick(position);
				return true;
			}
		});
		gv_booklist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (imageUrl != null && imageUrl.size() > 0 && position == 0) {
					return;
				}
				int index = (imageUrl != null && imageUrl.size() > 0) ? position - 1 : position;
				if (list.get(index).getIsRead() == 1) {
					if (adapter.isShowCheckBox()) {
						adapter.selectedItem(list.get(index).getBookId());
						adapter.notifyDataSetChanged();
					}
				}else if (list.get(index).getIsRead() == 2){
					Intent intent = new Intent(getActivity(), CollectReadActivity.class);
					startActivityForResult(intent, 660);
				}
			}
		});
		tv_all_choose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (adapter != null && adapter.isShowCheckBox()) {
					adapter.selectAllItem();
					adapter.notifyDataSetChanged();
				}
			}
		});
		tv_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (adapter != null && adapter.isShowCheckBox()) {
					adapter.clearSelected();
					adapter.notifyDataSetChanged();
				}
			}
		});
		tv_all_delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ArrayList<Integer> idList = adapter.getSelectedBookIds();
				if (idList != null && idList.size() > 0) {
					deleteRecord(idList);
				}else {
					CommonMethod.makeNotice(getActivity(), "请选择要删除的记录");
				}
			}
		});
		tv_operator_finish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (adapter != null && adapter.isShowCheckBox()) {
					adapter.setShowCheckBox(false);
					operator(false);
					adapter.clearSelected();
					adapter.notifyDataSetChanged();
					if ((imageUrl != null && imageUrl.size() == 0) && list.size() == 0) {
						rel_empty.setVisibility(View.VISIBLE);
					}else {
						rel_empty.setVisibility(View.GONE);
						if (imageUrl != null && imageUrl.size() > 0 && list.size() == 0) {
							rel_all_has_read.setVisibility(View.VISIBLE);
						}else {
							rel_all_has_read.setVisibility(View.GONE);
						}
					}
				}
			}
		});
	}
	
	private void longClick(int position){
		if (position == 0 && imageUrl != null && imageUrl.size() > 0) {
			return;
		}
		int index = (imageUrl != null && imageUrl.size() > 0) ? position - 1 : position;
		if (adapter != null && !adapter.isShowCheckBox() && list.get(index).getIsRead() == 1) {
			operator(true);
			adapter.setShowCheckBox(true);
			adapter.addSelected(list.get(index).getBookId());
			adapter.notifyDataSetChanged();
			handle.obtainMessage(2, 1, 0).sendToTarget();
		}
	}
	
	public void notifyUI(){
		if (type == 1 && list != null && list.size() > 0) {
			MusicPlayManager manager = MusicPlayManager.getInstance(getActivity());
			int id_s[] = manager.isPlayingBookId();
			if (id_s[0] > 0 && list.get(0).getBookId() != id_s[0]) {
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getBookId() > 0) {
						if (list.get(i).getBookId() == id_s[0]) {
							BookBean mBookBean = list.remove(i);
							if (list.size() > 0) {
								list.add(0, mBookBean);
							}else {
								list.add(mBookBean);
							}
							notifySort(mBookBean.getBookId());
							break;
						}
					}else {
						break;
					}
				}
			}
		}
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}
	
	private void notifySort(final int bookId){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				if (bookId > 0) {
					try {
						CacheTypeBean cacheTypeBean = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("bookId", bookId).and().eq("type", 1).queryForFirst();
						if (cacheTypeBean != null) {
							cacheTypeBean.delete();
							CacheTypeBean cacheTypeBean2 = new CacheTypeBean();
							cacheTypeBean2.setBookId(bookId);
							cacheTypeBean2.setType(1);
							StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).create(cacheTypeBean2);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
					
				}				
			}
		});
	}
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterEvent();
	}
	
	private void operator(boolean show){
		rel_top.setVisibility(show ? View.VISIBLE : View.GONE);
		rel_x.setVisibility(show ? View.VISIBLE : View.GONE);
	}
	
	@Subscribe
	public void onEventDownLoadComplete(Boolean boo) {
		getDownLoadList();
	}
	
	@Subscribe
	public void onEventDownLoadRefreshWhenListenChanged(DownLoadRefreshEvent event){
		getDownLoadList();
	}
	
	@Subscribe
	public void onEventChangeSort(Integer bId) {
		if (type == 1 && list != null && list.size() > 0) {
			if (bId > 0 && list.get(0).getBookId() != bId) {
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getBookId() > 0) {
						if (list.get(i).getBookId() == bId) {
							BookBean mBookBean = list.remove(i);
							if (list.size() > 0) {
								list.add(0, mBookBean);
							}else {
								list.add(mBookBean);
							}
							notifySort(mBookBean.getBookId());
							break;
						}
					}else {
						break;
					}
				}
			}
		}
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	
	}
	
	public void registerEvent(){
		EventBus.getEventBus("download_complete", ThreadEnforcer.MAIN).register(this);
		EventBus.getEventBus("m_listen_statue_change",ThreadEnforcer.MAIN).register(this);
		EventBus.getEventBus("m_refresh_collect_sort",ThreadEnforcer.MAIN).register(this);
	}
	
	public void unregisterEvent(){
		EventBus.getEventBus("download_complete", ThreadEnforcer.MAIN).unregister(this);
		EventBus.getEventBus("m_listen_statue_change",ThreadEnforcer.MAIN).unregister(this);
		EventBus.getEventBus("m_refresh_collect_sort",ThreadEnforcer.MAIN).unregister(this);
	}
	
	public void deleteRecord(final ArrayList<Integer> list){
		Pool.getPool().execute(new Runnable() {
			
			@Override
			public void run() {
				for (Integer integer : list) {
					try {
						CacheTypeBean cacheTypeBean = StaticReference.mutilDBSupport.getDao(CacheTypeBean.class).queryBuilder().where().eq("bookId", integer).and().eq("type", 1).queryForFirst();
						if (cacheTypeBean != null) {
							cacheTypeBean.delete();
							if (TemporaryVariable.downLoadedList.contains(integer)) {
								TemporaryVariable.downLoadedList.remove(TemporaryVariable.downLoadedList.indexOf(integer));
							}
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}		
				handle.obtainMessage(DELETECACHE_SUCCESS,list).sendToTarget();
			}
		});
	}
	
	
	/**
	 * 下載列表
	 * @param type isRead
	 */
	public void getDownLoadList(){
		Pool.getPool().execute(new Runnable() {
			@Override
			public void run() {
				try {
					GenericRawResults<BookBean> result = StaticReference.mutilDBSupport.getDao(BookBean.class)
							.queryRaw("select a.about,a.author,a.slogan,a.bookName,a.commentNum,a.createTime,a.nationImg, a.faceImg, a.faceimgplay,a.favorNum,a.goldword,a.listenNum,a.isfavor,a.ensize,a.prosize,a.doubisize,a.bookid,a.isRead,a.isNew from book a inner join  cachetype b on a.bookId = b.bookId and b.type = 1 " + ((type == 1) ? "" : " and a.isRead = 2 ")+  " order by b.tId desc",
									new RawRowMapper<BookBean>(){
										@Override
										public BookBean mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
											BookBean bookBean = new BookBean();
											bookBean.setAbout(resultColumns[0]);
											bookBean.setAuthor(resultColumns[1]);
											bookBean.setSlogan(resultColumns[2]);
											bookBean.setBookName(resultColumns[3]);
											bookBean.setCommentNum(resultColumns[4]);
											bookBean.setCreateTime(new Date());
											bookBean.setNationImg(resultColumns[6]);
											bookBean.setFaceImg(resultColumns[7]);
											bookBean.setFaceImgPlay(resultColumns[8]);
											bookBean.setFavorNum(resultColumns[9]);
											bookBean.setGoldWord(resultColumns[10]);
											bookBean.setListenNum(resultColumns[11]);
											bookBean.setIsFavor(Integer.parseInt(resultColumns[12]));
											bookBean.setEnSize(Integer.parseInt(resultColumns[13]));
											bookBean.setProSize(Integer.parseInt(resultColumns[14]));
											bookBean.setDoubiSize(Integer.parseInt(resultColumns[15]));
											bookBean.setBookId(Integer.parseInt(resultColumns[16]));
											int inRead = 1;
											try {
												inRead = Integer.parseInt(resultColumns[17]);
											} catch (Exception e) {
												e.printStackTrace();
											}
											bookBean.setIsRead(inRead);
											
											int isNew = 1;
											try {
												isNew = Integer.parseInt(resultColumns[18]);
											} catch (Exception e) {
												e.printStackTrace();
											}
											bookBean.setIsNew(isNew);
											
											return bookBean;
										}});
					
					List<BookBean> bookList = result.getResults();
					for (int i = 0; i < bookList.size(); i++) {
						List<SelectionVo> selectionList = StaticReference.mutilDBSupport.getDao(SelectionVo.class).queryBuilder().where().eq("bookId", bookList.get(i).getBookId()).query();
						if (selectionList != null) {
							bookList.get(i).setSectionList(selectionList);
						}
					}
					handle.obtainMessage(GETCACHE_SUCCESS, bookList).sendToTarget();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void showDialog(final BookBean bookBean){
		CommonAlertDialog alertDialog = new CommonAlertDialog(getActivity());
		alertDialog.setTextMessage(String.format(getResources().getString(R.string.play_size_notice), CommonMethod.calculationNum(bookBean.getProSize()+bookBean.getEnSize() + bookBean.getDoubiSize())));
		alertDialog.setShowTitle(true);
		String[] a = {getResources().getString(R.string.app_continue),getResources().getString(R.string.app_cancel)};
		alertDialog.setBtns(new DialogListener() {
			
			@Override
			public void okButtonClick(Dialog dialog) {
				play(bookBean);
				MusicPlayManager.getInstance(getActivity()).setShowNetTips(false);
				dialog.dismiss();
			}

			@Override
			public void cancelButtonClick(Dialog dialog) {
				CommonMethod.reordPlayStatus(getActivity(), bookBean.getBookId(), true);
				dialog.dismiss();
			}

			@Override
			public void otherButtonClick(Dialog dialog) {
				CommonMethod.reordPlayStatus(getActivity(), bookBean.getBookId(), true);
				dialog.dismiss();
			}
		},a);
		alertDialog.show();
	}
	
	
	public void playDialog(final BookBean bookBean){
		if (TemporaryVariable.downLoadedList.contains(bookBean.getBookId())) {
			play(bookBean);
		}else {
			if ((boolean)SharedPreferencesUtils.getParam(getActivity(), HttpRequestHelper.DWONLOAD_PLAY_CONTINUE, false)) {
				play(bookBean);
			} else {
				switch (CommonMethod.getNetworkState(getActivity())) {
				case 0:
					CommonMethod.makeNotice(getActivity(), getActivity().getResources().getString(R.string.connect_err_io));
					break;
				case 1:
					play(bookBean);
					break;
				case 2:
					showDialog(bookBean);
					break;
				}
			}
		}
	}
	
	
	private void play(final BookBean bookBean){
		handle.postDelayed(new Runnable() {
			public void run() {
				ArrayList<BookBean> m_list = new ArrayList<BookBean>();
				int index = list.indexOf(bookBean);
				m_list.add(bookBean);
				for (int k = index - 1; k >= 0; k-- ) {
					if (list.get(k).getBookId() > 0 && list.get(k).getIsRead() == 1) {
						m_list.add(0,list.get(k));
					} else {
						break;
					}
				}
				for (int i = index + 1; i < list.size(); i++) {
					if (list.get(i).getBookId() > 0  && list.get(i).getIsRead() == 1) {
						m_list.add(list.get(i));
					} else {
						break;
					}
				}
				MusicPlayManager.getInstance(getActivity()).setPlayList(m_list).setBookId(bookBean.getBookId()).play();
			}
		}, 1500);
		TemporaryVariable.read_list.clear();
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getBookId() > 0) {
				TemporaryVariable.read_list.add(list.get(i).getBookId());
			}
		}
		if (TemporaryVariable.playActivity != null && TemporaryVariable.playActivity instanceof PlayActivity) {
			TemporaryVariable.playActivity.finish();
		}
		Intent intent = new Intent(getActivity(), PlayActivity.class);
		intent.putExtra("bookId", bookBean.getBookId());
		intent.putExtra("faceImg", bookBean.getFaceImg());
		intent.putExtra("canRead", bookBean.getCanRead());
		intent.putExtra("needBuy", bookBean.getNeedBuy());
		getActivity().startActivity(intent);
	}
}
